<?php
session_start();
if(!isset($_SESSION['id'])){
    header('Location: login.php');
    exit;
}

include_once("../controller/funcionario_controller_class.php");
include_once("../controller/especialidade_controller_class.php");
include_once("../controller/equipe_controller_class.php");
include_once("../controller/ocorrencia_controller_class.php");

$objEquipe = new EquipeController();
$objEspec = new EspecialidadeController();
$listEspec = $objEspec->selecionar();

$objFunc = new FuncionarioController();



if(!isset($_POST['cadastrar'])){
    
    @$id = $_GET['id'];
    @$urgencia = $_GET['urgencia'];
    
}else{
    @$id = $_POST['id'];
    @$equipe = $_POST['list2'];
    @$urgencia = $_POST['urgencia'];
    $objOcorrencia = new OcorrenciaController();
    
    if(empty($equipe)){
        echo "<script>alert('Selecione ao menos um funcionario')</script>";
    }else{
    
        if($objOcorrencia->montarEquipeSimples($id, $equipe, 4)){
            
            echo "<script>alert('Cadastrado com Sucesso')</script>";
            echo "<script>opener.location.reload()</script>";
            echo "<script>window.self.close()</script>";
            
        }else{
            echo "<script>alert('Erro')</script>";
        }
    }
}

$listFunc = $objFunc->selecionarParaEquipe($urgencia); 

?>
<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8" />
        <link rel="shortcut icon" href="img/logo.jpg" />
        <link rel="stylesheet" type="text/css" href="css/normalize.css" />
        <link rel="stylesheet" type="text/css" href="css/demo.css" />
        <link rel="stylesheet" type="text/css" href="css/icons.css" />
        <link rel="stylesheet" type="text/css" href="css/style2.css" />
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <link rel="stylesheet" type="text/css" href="css/component.css" />
        <link rel="stylesheet" type="text/css" href="css/shadowbox.css" />
        <link href="css/StyleOS.css" type="text/css" rel="stylesheet" />
        <link href="css/style_light.css" type="text/css" rel="stylesheet" />
        <script type="text/javascript" src="js/jquery1.6.min.js"></script>


        <script language="JavaScript">
            function move(MenuOrigem, MenuDestino) {
                var arrMenuOrigem = new Array();
                var arrMenuDestino = new Array();
                var arrLookup = new Array();
                var i;
                for (i = 0; i < MenuDestino.options.length; i++) {
                    arrLookup[MenuDestino.options[i].text] = MenuDestino.options[i].value;
                    arrMenuDestino[i] = MenuDestino.options[i].text;
                }
                var fLength = 0;
                var tLength = arrMenuDestino.length;
                for (i = 0; i < MenuOrigem.options.length; i++) {
                    arrLookup[MenuOrigem.options[i].text] = MenuOrigem.options[i].value;
                    if (MenuOrigem.options[i].selected && MenuOrigem.options[i].value != "") {
                        arrMenuDestino[tLength] = MenuOrigem.options[i].text;
                        tLength++;
                    }
                    else {
                        arrMenuOrigem[fLength] = MenuOrigem.options[i].text;
                        fLength++;
                    }
                }
                arrMenuOrigem.sort();
                arrMenuDestino.sort();
                MenuOrigem.length = 0;
                MenuDestino.length = 0;
                var c;
                for (c = 0; c < arrMenuOrigem.length; c++) {
                    var no = new Option();
                    no.value = arrLookup[arrMenuOrigem[c]];
                    no.text = arrMenuOrigem[c];
                    MenuOrigem[c] = no;
                }
                for (c = 0; c < arrMenuDestino.length; c++) {
                    var no = new Option();
                    no.selected = true;
                    no.value = arrLookup[arrMenuDestino[c]];
                    no.text = arrMenuDestino[c];
                    MenuDestino[c] = no;
                }
            }
        </script>
        
        
    </head> 
    <style>
        .box{
            width:300px;	
            height:300px;
            border:solid 1px;

        }
        .form-equipe{
            border:0px;	

        }
        .botao {
            background: #3498db;
            background-image: -webkit-linear-gradient(top, #3498db, #2980b9);
            background-image: -moz-linear-gradient(top, #3498db, #2980b9);
            background-image: -ms-linear-gradient(top, #3498db, #2980b9);
            background-image: -o-linear-gradient(top, #3498db, #2980b9);
            background-image: linear-gradient(to bottom, #3498db, #2980b9);
            -webkit-border-radius: 0;
            -moz-border-radius: 0;
            border-radius: 0px;
            text-shadow: 1px 1px 3px #666666;
            -webkit-box-shadow: 0px 1px 3px #666666;
            -moz-box-shadow: 0px 1px 3px #666666;
            box-shadow: 0px 1px 3px #666666;
            font-family: Arial;
            color: #ffffff;
            font-size: 12px;
            padding: 9px 17px 10px 20px;
            border: solid #1f628d 1px;
            text-decoration: none;
        }

        .botao:hover {
            background: #3cb0fd;
            background-image: -webkit-linear-gradient(top, #3cb0fd, #3498db);
            background-image: -moz-linear-gradient(top, #3cb0fd, #3498db);
            background-image: -ms-linear-gradient(top, #3cb0fd, #3498db);
            background-image: -o-linear-gradient(top, #3cb0fd, #3498db);
            background-image: linear-gradient(to bottom, #3cb0fd, #3498db);
            text-decoration: none;
        }

        .clear{
            margin-top:2%;

        }

    </style>

    <body style="overflow-x: hidden">
        <form name="montarEquipe" id="montarEquope" action="montarEquipe.php" method="post" class="form-equipe">
            <fieldset>
                <label for="txtEspecialidade">Escolha a especialidade: </label>
                <select name="txtEspecialidade" id="txtEspecialidade"> 
                    <option value="0">Selecione</option>
                    <?php if (count($listEspec) > 0) { ?>
                        <?php foreach ($listEspec as $espec) { ?>
                            <option value="<?= $espec->getId() ?>" > <?= $espec->getNome() ?></option>
                        <?php
                        }
                    }
                    ?>
                </select><br /><br />
                <div class="clear"></div>
                <table border="0">

                    <tr>
                    <label for="nomes"> Nomes: </label>
                    <label for="formaEquipe" style="margin-left:390px"> Formar Equipe: </label>
                    </tr>

                    <tr>
                        <td>
                            <select multiple name="list1[]" class="box" id="list1">
                                <?php if (count($listFunc) > 0) { ?>
                                <?php foreach ($listFunc as $func) { ?>
                                <option value="<?= $func->getId() ?>" > 
                                    <?= $func->getNome() ?>
                                    <?php if($objEquipe->possueEquipe($func->getId()) != -1){
                                        echo "- <i>Ocupado</i>";
                                    } ?>
                                </option>
                                <?php } }?>
                            </select>
                        </td>

                        <td align="center">

                            <input type="button" onClick="move(this.form.list2, this.form.list1)" value="<< Remover Nome" title="Remover"><br />
                            <input type="button" onClick="move(this.form.list1, this.form.list2)" value="Adicionar Nome >>" title="Adicionar">
                        </td>

                        <td>
                            
                            <select multiple name="list2[]" class="box" id="list2">
                                
                            </select>

                        </td>
                    </tr>

                </table>
                <div class="clear"></div>
                <input type="hidden" name="id" value="<?= $id ?>">
                <input type="hidden" name="urgencia" value="<?= $id ?>">
                <input type="submit" value="Gravar Equipe" id="cadastrar" name="cadastrar" class="botao" style="margin-left:500px;" />&nbsp;
                <input type="button" value="Cancelar" class="botao" onClick="javascript:c = window.confirm('Deseja Realmente cancelar?');
                        if (c == true) {
                            self.close();
                        } else {
                        }" />
            </fieldset>
        </form>
    </body>
</html>
